package com.tl.commonlibrary.network;

import android.text.TextUtils;

import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.BuildConfig;
import com.tl.commonlibrary.tool.Logger;
import com.tl.commonlibrary.ui.user.Account;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2017/6/2 0002.
 */

public class NetManager {

    private volatile static NetManager netManager;
    private Retrofit retrofit;

    private NetManager() {
        retrofit = getRetrofit(NetConfig.API_URL);
    }

    public static Retrofit getRetrofit(String url) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(AppConfig.DEBUG() ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
        builder.addInterceptor(loggingInterceptor);
//        builder.addInterceptor(new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request request = chain.request();
//                Request newRequest = request.newBuilder()
//                        .header("uid", Account.getUid())
//                        .header("token", Account.getToken())
//                        .method(request.method(), request.body())
//                        .build();
////                Logger.e("Http", "uid=" + Account.getUid());
////                Logger.e("Http", "token=" + Account.getToken());
//                return chain.proceed(newRequest);
//            }
//        });
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create()).client(builder.build()).build();
        return retrofit;
    }

    public void setBaseUrl(String url) {
        retrofit = getRetrofit(url);
    }

    public static NetManager get() {
        if (netManager == null) {
            synchronized (NetManager.class) {
                if (netManager == null) {
                    netManager = new NetManager();
                }
            }
        }
        return netManager;
    }

//    public static OkHttpClient getOkHttp() {
//
////        builder.addInterceptor(new RequestInterceptor(new CommonParam() {
////            @Override
////            public String getCommonParam() {
////                return null;
////            }
////        }));
//        return builder.build();
//    }

    public <T> T create(Class<T> clazz) {
        return retrofit.create(clazz);
    }

    public void onDestroy() {
        retrofit = null;
        netManager = null;
    }
}
